package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.Color;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import model.*;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class OrderOverviewProductListPanel extends JPanel
{
    private OrderOverview orderOverview;

    public OrderOverviewProductListPanel(OrderOverview view, final OrderLine orderLine)
    {
        orderOverview = view;
        
        setLayout(null);
        setBounds(0, 0, 500, 25);
        setOpaque(false);
        
        JButton btnRemove = new JButton("x");
        btnRemove.setBounds(0, 1, 23, 23);
        btnRemove.setBackground(Color.WHITE);
        btnRemove.setForeground(DesignLayout.T_FIELD_BORDER_RED);
        btnRemove.setFont(DesignLayout.STANDARD.deriveFont(11f));
        btnRemove.setBorder(BorderFactory.createEmptyBorder());
        btnRemove.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                orderOverview.getOrderContainer().getOrder().getOrderObject().removeOrderLine(orderLine);
                try
                {
                    if (orderLine.getOrder().getId() == 0)
                    {
                        control.ProductControl.setProductSold(orderLine.getProduct(), false);
                    }
                } 
                catch (SQLException ex)
                {
                    ExceptionManager.handleSQLException(ex);
                } 
                catch (ClassNotFoundException ex)
                {
                    ExceptionManager.handleClassNotFoundException(ex);
                }
            }
        });
        
        JLabel lblProductIDAndName = new JLabel("<html><b>"+orderLine.getProduct().getId()+"</b> "+orderLine.getProduct().getName()+"</html>");
        lblProductIDAndName.setBounds(25, 0, 500, 25);
        lblProductIDAndName.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JTextField tFieldAmount = new JTextField(""+orderLine.getAmount());
        tFieldAmount.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldAmount.setBounds(300, 0, 50, 25);
        tFieldAmount.setFont(DesignLayout.STANDARD.deriveFont(11f));
        tFieldAmount.setOpaque(false);
        tFieldAmount.setBackground(Color.WHITE);
        tFieldAmount.setBorder(null);
        tFieldAmount.addFocusListener(new FocusListener() 
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                JTextField field = (JTextField) e.getComponent();
                field.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                JTextField field = (JTextField) e.getComponent();
                field.select(0, 0);
            }
        });
        if (orderLine.getProduct().isRemainAfterSale() == false)
        {
            tFieldAmount.setEditable(false);
        }
        tFieldAmount.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JTextField theSource = (JTextField) e.getSource();
                try
                {
                    orderLine.setAmount(Integer.parseInt(theSource.getText()));
                } 
                catch (NumberFormatException evt)
                {
                    theSource.setText(""+orderLine.getAmount());                    
                }
            }
        });
        
        JTextField tFieldUnitPrice = new JTextField(""+orderLine.getUnitPrice());
        tFieldUnitPrice.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldUnitPrice.setBounds(350, 0, 50, 25);
        tFieldUnitPrice.setFont(DesignLayout.STANDARD.deriveFont(11f));
        tFieldUnitPrice.setOpaque(false);
        tFieldUnitPrice.setBackground(Color.WHITE);
        tFieldUnitPrice.setBorder(null);
        tFieldUnitPrice.addFocusListener(new FocusListener() 
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                JTextField field = (JTextField) e.getComponent();
                field.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                JTextField field = (JTextField) e.getComponent();
                field.select(0, 0);
            }
        });
        if (orderLine.getProduct().isRemainAfterSale() == false)
        {
            tFieldUnitPrice.setEditable(false);
        }
        tFieldUnitPrice.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JTextField theSource = (JTextField) e.getSource();
                try
                {
                    orderLine.setUnitPrice(Double.parseDouble(theSource.getText()));
                } 
                catch (NumberFormatException evt)
                {
                    theSource.setText(""+orderLine.getUnitPrice());                    
                }                
            }
        });
        
        JLabel lblPrice = new JLabel(""+(orderLine.getAmount() * orderLine.getUnitPrice()), JLabel.CENTER);
        lblPrice.setBounds(400, 0, 100, 20);
        lblPrice.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        add(btnRemove);
        add(lblProductIDAndName);
        add(tFieldAmount);
        add(tFieldUnitPrice);
        add(lblPrice);
    }
}